//
//  MADareView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/13.
//

#import "MADareView.h"
#import "MADareRoundView.h"
#import "MADareChoiceView.h"
#import "MADareResultView.h"

@interface MADareView ()

@property (nonatomic, strong) MADareRoundView *roundView;

@property (nonatomic, strong) UILabel *fLabel;
@property (nonatomic, strong) UILabel *mLabel;

@property (nonatomic, strong) UIButton *beginBtn;


@end

@implementation MADareView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface {
    _roundView = [MADareRoundView new];
    _roundView.clipsToBounds = YES;
    [self addSubview:_roundView];
    [_roundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.height.ma_offset(512);
        make.left.right.mas_equalTo(0);
    }];
    
    NSUInteger value = arc4random() % 1;
    
    _fLabel = [UILabel new];
    _fLabel.text = @"女士";
    _fLabel.font = MAFontScaleMedium(19);
    _fLabel.textAlignment = NSTextAlignmentCenter;
    _fLabel.backgroundColor = MAColor(@"#FF0071");
    _fLabel.layer.borderColor = UIColor.whiteColor.CGColor;
    _fLabel.layer.borderWidth = 1;
    _fLabel.layer.cornerRadius = MARatioWidth(52);
    _fLabel.clipsToBounds = YES;
    _fLabel.textColor = UIColor.whiteColor;
    _fLabel.numberOfLines = 0;
//    _fLabel.adjustsFontSizeToFitWidth = YES;
 
    _mLabel = [UILabel new];
    _mLabel.text = @"男士";
    _mLabel.font = MAFontScaleMedium(19);
    _mLabel.textAlignment = NSTextAlignmentCenter;
    _mLabel.textColor = UIColor.whiteColor;
    _mLabel.backgroundColor = MAColor(@"#38AEFF");
    _mLabel.layer.borderColor = UIColor.whiteColor.CGColor;
    _mLabel.layer.borderWidth = 1;
    _mLabel.layer.cornerRadius = MARatioWidth(52);
    _mLabel.clipsToBounds = YES;
    _mLabel.numberOfLines = 0;
//    _mLabel.adjustsFontSizeToFitWidth = YES;
    if (value == 0) {
        [self addSubview:_fLabel];
        [self addSubview:_mLabel];
    } else {
        [self addSubview:_mLabel];
        [self addSubview:_fLabel];
    }
    
    [_mLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(8);
        make.size.ma_offset(104);
        make.centerY.mas_equalTo(0);
    }];
    [_fLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.ma_offset(-8);
        make.size.ma_offset(104);
        make.centerY.mas_equalTo(0);
    }];
    
    _beginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _beginBtn.layer.cornerRadius = MARatioWidth(64);
    _beginBtn.layer.borderWidth = 3;
    _beginBtn.layer.borderColor = UIColor.whiteColor.CGColor;
    _beginBtn.clipsToBounds = YES;
    [_beginBtn setBackgroundColor:MAColor(@"#FF1444")];
    [_beginBtn.titleLabel setFont:MAFontSemibold(39)];
    [_beginBtn setTitle:@"开始" forState:UIControlStateNormal];
    [_beginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [_beginBtn addTarget:self action:@selector(choiceAnimationEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_beginBtn];
    [_beginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.ma_offset(128);
    }];
    
    
}

- (void)setFText:(NSString *)fText {
    _fLabel.text = _fText = fText;
}

- (void)setMText:(NSString *)mText {
    _mLabel.text = _mText = mText;
}

- (void)setType:(NSUInteger)type {
    _type = type;
    if (type == 0) {
        _fLabel.backgroundColor = MAColor(@"#26A5FC");
        _mLabel.backgroundColor = MAColor(@"#26A5FC");
    } else if (type == 1) {
        _fLabel.backgroundColor = MAColor(@"#FF0071");
        _mLabel.backgroundColor = MAColor(@"#26A5FC");
    } else if (type == 2) {
        _fLabel.backgroundColor = MAColor(@"#FF0071");
        _mLabel.backgroundColor = MAColor(@"#FF0071");
    }
}

- (void)roundAnimationEvent {
    [_roundView animationEvent];
}

- (void)choiceAnimationEvent {
    MAWeakify(self);
    [UIView animateWithDuration:0.5 animations:^{
        MAStrongify(self);
        self.beginBtn.alpha = 0;
    }];
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.75 initialSpringVelocity:5.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        MAStrongify(self);
        [self.fLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.ma_offset(8);
            make.size.ma_offset(104);
            make.centerY.mas_equalTo(0);
        }];
        [self.mLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.ma_offset(-8);
            make.size.ma_offset(104);
            make.centerY.mas_equalTo(0);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        MAStrongify(self);
        [self exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
        [self showChoiceView];
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.75 initialSpringVelocity:5.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
            MAStrongify(self);
            [self.fLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.size.ma_offset(104);
                make.center.mas_equalTo(0);
            }];
            [self.mLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.size.ma_offset(104);
                make.center.mas_equalTo(0);
            }];
            [self layoutIfNeeded];
        } completion:nil];
    }];
    
}

- (void)showChoiceView {
    MABlock(self.showChoiceBlock);
    MADareChoiceView *view = [MADareChoiceView new];
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self layoutIfNeeded];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [view beginAnimation];
    });
    MAWeakify(self);
    view.choiceBlock = ^(NSUInteger type) {
        MAStrongify(self);
        MABlock(self.choiceBlock, type);
    };
}

- (void)showResultView:(NSString *)rule {
    MADareResultView *resultView = [MADareResultView new];
    if ([self.subviews indexOfObject:_fLabel] > [self.subviews indexOfObject:_mLabel]) {
        resultView.fName = _fText;
        resultView.mName = _mText;
    } else {
        resultView.fName = _mText;
        resultView.mName = _fText;
    }
    resultView.rule = rule;
    [self addSubview:resultView];
    [resultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self);
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.mas_bottom);
    }];
    [self layoutIfNeeded];
    MAWeakify(self);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            MAStrongify(self);
            [resultView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(self);
                make.centerX.top.mas_equalTo(0);
            }];
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            MAStrongify(self);
            self.beginBtn.alpha = 1;
            [self.mLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.ma_offset(8);
                make.size.ma_offset(104);
                make.centerY.mas_equalTo(0);
            }];
            [self.fLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.ma_offset(-8);
                make.size.ma_offset(104);
                make.centerY.mas_equalTo(0);
            }];
            [self layoutIfNeeded];
        }];
    });
    MAWeakify(resultView);
    resultView.closeBlock = ^{
        MAStrongify(self);
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            MAStrongify(self);
            MAStrongify(resultView);
            [resultView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(self);
                make.centerX.mas_equalTo(0);
                make.top.mas_equalTo(self.mas_bottom);
            }];
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            MAStrongify(self);
            MAStrongify(resultView);
            [resultView removeFromSuperview];
            MABlock(self.endBlock);
        }];
    };
}

@end
